﻿using System;
using System.Windows.Forms;
using Curriculum.DataSet;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress;

namespace Curriculum.Controls
{
    public partial class VacanciesXtraUserControl : XtraUserControl, IGridView, IList
    {
        private BackOfficeController _controller;

        #region Properties

        public BackOfficeController Controller
        {
            set
            {
                _controller = value;
                //SetGridDataSource();
                _controller.VacancySaved += delegate { SetGridDataSource(); };
            }
        }

        #endregion

        public VacanciesXtraUserControl()
        {
            InitializeComponent();
            gridView.DoubleClick += GridViewDoubleClick;
            deleteRepositoryItemButtonEdit.ButtonClick += DeleteRepositoryItemButtonEditButtonClick;
        }

        public void Reload()
        {
            SetGridDataSource();
        }

        #region Private Methods

        public string GetExtendedFilter()
        {
            if (ReferenceEquals(gridView.ActiveFilterCriteria, null))
                return "";
            return gridView.ActiveFilterCriteria.LegacyToString();
        }

        private void SetGridDataSource()
        {
            var dataSet = _controller.GetVacancies(GetExtendedFilter());
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Vacancy.TableName;
        }

        private void ShowVacancy(long documentId)
        {
            var window = new VacancyXtraForm { DocumentId = documentId, Controller = _controller };
            window.ShowDialog();
        }

        #endregion

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "Vacancies"; }
        }

        #endregion

        public void AddDocument()
        {
            ShowVacancy(-1);
        }

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            var vacancy = (VacancyDataSet.VacancyRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (vacancy == null)
                return;
            ShowVacancy(vacancy.Id);
        }        

        void DeleteRepositoryItemButtonEditButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var vacancy = (VacancyDataSet.VacancyRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (vacancy == null)
                return;
            if(!_controller.CanDeleteVacancy(vacancy.Id))
            {
                MessageBox.Show(this, "Não é possível eliminar a vaga, dado que tem existem candidatos associados À mesma.", "Vaga", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            _controller.DeleteVacancy(vacancy.Id);            
        }
    }
}
